<?php

class Admin_m extends Model {

    //put your code here
    function Admin_m() {
        parent::Model();
        $this->load->database();
    }

    function login($email, $pass) {
        $this->db->select();
        $this->db->from('lehu_users');
        $this->db->where(array('email' => $email));
        $this->db->where(array('password' => $pass));
        $this->db->where('group_id <',3);
        $query = $this->db->get();
        return $query;
    }

    function get_admin_details($id) {
        $this->db->select('lehu_users.email,
            lehu_profiles.display_name,
            lehu_profiles.first_name,
            lehu_users.id,
            lehu_profiles.last_name,
            lehu_profiles.company,
            lehu_profiles.dob,
            lehu_profiles.gender,
            lehu_profiles.phone,
            lehu_profiles.mobile,
            lehu_profiles.address,
            lehu_profiles.gravatar
                        ');
        $this->db->from('lehu_users ,lehu_profiles');
        $this->db->where('lehu_users.id = lehu_profiles.user_id');
        $this->db->where('lehu_users.id', $id);
        return $this->db->get();
    }

    function edit_profile($data, $id) {
        return $this->db->update('lehu_profiles', $data, array('user_id' => $id));
    }

    function change_pass($data, $id) {
        return $this->db->update('lehu_users', $data, array('id' => $id));
    }

    function is_password($pass, $idadmin) {
        $query = $this->db->get_where('lehu_users', array('id' => $idadmin, 'password' => $pass));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }

    function count_user($level){
        $this->db->select('lehu_users.id,lehu_users.email,lehu_users.username,lehu_profiles.first_name,lehu_profiles.last_name');
        $this->db->from('lehu_users ,lehu_profiles');
        $this->db->where('lehu_users.id = lehu_profiles.user_id');
        $this->db->where('lehu_users.group_id = ',$level);
        $query = $this->db->get();
        return count($query->result_array());
    }

    function get_all_user($per_page,$off_set,$level){
        $this->db->select('lehu_users.id,lehu_users.active,lehu_users.email,lehu_users.username,lehu_profiles.first_name,lehu_profiles.last_name');
        $this->db->from('lehu_users ,lehu_profiles');
        $this->db->where('lehu_users.id = lehu_profiles.user_id');
        $this->db->where('lehu_users.group_id = ',$level);
        $this->db->limit($per_page,$off_set);
        $query = $this->db->get();
        return $query->result_array();
    }
    function delete_admin($id){
        $this->db->delete('lehu_profiles',array('user_id'=>$id));
         return $this->db->delete('lehu_users',array('id'=>$id));
     }

     function list_all_contacts($per_page, $off_set) {
        $this->db->select();
        $this->db->from('lehu_contacts');
        $this->db->limit($per_page, $off_set);
        $this->db->order_by('id DESC');
        $query = $this->db->get();
        return $query->result_array();
    }

    function count_all_contacts() {
        $this->db->select();
        $this->db->from('lehu_contacts');
        $query = $this->db->get();
        return count($query->result_array());
    }

    function read_contact($id) {
        $this->db->select();
        $this->db->from('lehu_contacts');
        $this->db->where('lehu_contacts.id = ', $id);
        $query = $this->db->get();
        return $query;
    }

    function delete_contact($id) {
        return $this->db->delete('lehu_contacts', array('id' => $id));
    }

    function get_propos(){
        $this->db->select();
        $this->db->from('lehu_propos');
        $this->db->where('lehu_propos.id = 1');
        $query = $this->db->get();
        return $query;
    }

    function edit_propos($data) {
        return $this->db->update('lehu_propos', $data,array('id' => 1));
    }

    function deactivate_user($id) {
        return $this->db->update('lehu_users',array('active' => 0), array('id' => $id));
    }

    function get_deactivate_user($email,$password){
        $this->db->select();
        $this->db->from('lehu_users');
        $this->db->where(array('email' => $email));
        $this->db->where(array('password' => $password));
        $query = $this->db->get();
        return $query;
    }
    function activate_user($id) {
        return $this->db->update('lehu_users',array('active' => 1), array('id' => $id));
    }
}

?>
